using System;
using System.Collections.Generic;
using System.Text;

// Add the following namespaces
using BattleCore;
using BattleCore.Events;
using System.Drawing;
using System.Collections;
using System.Data;

namespace Behaviors
{
    // Add the attribute and the base class
    [Behavior("BotTest", "true", "0.1", "udp", "Testing misc stuff")]

    public class BotTest : BotEventListener
    {
        private PlayerPositionEvent myPosition = new PlayerPositionEvent();

        private ushort ownID;

        private String attachedTo = null;
        private int num = 0;

        // Bot Test
        public BotTest()
        {
            //Command registration
            RegisterCommand("!attach", HandleAttach);
            RegisterCommand("!detach", HandleDetach);

            RegisterTimedEvent("position", 250, HandlePositionRefresh);
        }

        // Upon receiving a PlayerPositionEvent from the server
        public void HandlePlayerPosition(object sender, PlayerPositionEvent e)
        {
            // If the bot is attached to somebody
            if ((this.attachedTo != null) && (e.PlayerName == this.attachedTo))
            {
                num = ((num + 1) % 5);

                // Echo every fifth packet...ghetto way of doing this, probably
                if (num == 0)
                {
                    myPosition.MapPositionX = e.MapPositionX;
                    myPosition.MapPositionY = e.MapPositionY;

                    SendGameEvent(myPosition);
                }

                if (e.Weapon.Type != WeaponTypes.NoWeapon)
                    Game(e);
            }
        }
        // Upon receiving a DestroyTurretEvent
        public void HandleDestroyTurret(object sender, DestroyTurretEvent e)
        {
            ChatEvent f = new ChatEvent();
            f.Message = "in bot...got DestroyTurretEvent from " + e.TurretHostName;
            f.ChatType = ChatTypes.Arena;
            SendGameEvent(f);

            // If we're attached to the player sending the DestroyTurretEvent...
            if ((this.attachedTo != null) && (e.TurretHostName == this.attachedTo))
            {
                // Create and send a DestroyTurretEvent, which will tell the server 
                //  that the bot has detached from the player
                DestroyTurretEvent dte = new DestroyTurretEvent();
                //dte.TurretHostId = 0xFFFF;    // hardcoded, not needed
                SendGameEvent(dte);

                // No longer attached to somebody
                this.attachedTo = null;
            }
        }

        // Every 250 ms, send out a position packet so the bot can stay in a ship
        public void HandlePositionRefresh()
        {
            if (this.attachedTo == null)
            {
                myPosition.MapPositionX = 500;
                myPosition.MapPositionY = 500;
                SendGameEvent(myPosition);
            }
        }

        // When a !attach command is received...
        public void HandleAttach(ChatEvent e)
        {
            // Create and send a new CreateTurretEvent to inform the server that the bot
            //  has attached to the player sending the command
            CreateTurretEvent cte = new CreateTurretEvent();
            cte.TurretHostName = e.PlayerName;
            SendGameEvent(cte);

            // Store who we're attached to, so we can detach later
            this.attachedTo = e.PlayerName;
        }

        // When a !detach command is received...
        public void HandleDetach(ChatEvent e)
        {
            // If we're attached to the player sending the command...
            if ((this.attachedTo != null) && (this.attachedTo == e.PlayerName))
            {
                // Create and send a DestroyTurretEvent, which will tell the server 
                //  that the bot has detached from the player
                DestroyTurretEvent dte = new DestroyTurretEvent();
                //dte.TurretHostId = 0xFFFF;    // hardcoded, not needed
                SendGameEvent(dte);

                // No longer attached to somebody
                this.attachedTo = null;
            }
        }

        // This method is required by the base class
        // perform any cleanup here
        public override void Dispose()
        {
        }

    }
}
